import ast

import astunparse as astunparse

import node_visitor


class CodeVisitor(ast.NodeVisitor):
    # def visit_BinOp(self, node):
    #     if isinstance(node.op, ast.Add):
    #         node.op = ast.Sub()
    #     self.generic_visit(node)

    def visit_FunctionDef(self, node):
        print('Function Name:%s' % node.name)

        self.generic_visit(node)
        # func_log_stmt = ast.Print(
        #     dest=None,
        #     values=[ast.Str(s='calling func: %s' % node.name, lineno=0, col_offset=0)],
        #     nl=True,
        #     lineno=0,
        #     col_offset=0,
        # )
        # node.body.insert(0, func_log_stmt)


func_def = \
    """
    def add(x, y):
        return x + y
    print(add(3, 5))
    """

# r_node = ast.parse(func_def)
file = open('get_author.py', 'r')
source = file.read()
r_node = ast.parse(source)
# visitor = CodeVisitor()
# visitor.visit(r_node)
# # print astunparse.dump(r_node)
# print(astunparse.unparse(r_node))
# # exec(compile(r_node, '<string>', 'exec'))

visitor = node_visitor.NodeVisitor('get_author.py')
# visitor.visit(r_node)
visitor.generic_visit(r_node)

body = r_node.body
body0 = body[0]
print(r_node.body)
print(visitor.context)
print(visitor.dangercalls)
